package cn.ultrarisk.scadmin.manager.web.controller.system;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.scadmin.manager.biz.common.dao.DictDAO;
import cn.ultrarisk.scadmin.manager.biz.common.entity.Dict;
import cn.ultrarisk.scadmin.manager.biz.common.service.DictService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.system.DictDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * DictController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends BaseController {

    @Autowired
    private DictDAO dictDAO;
    @Autowired
    private DictService dictService;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<Dict>> list(String name, String remark, int pageNo, int pageSize) {
        Page<Dict> dicts = dictDAO.findPage(name, remark, pageNo, pageSize);
        return getOkResponseEntity(dicts);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody DictDTO dto) {
        dictService.create(dto.getName(), dto.getType(), dto.getRemark(), dto.getValue(), Dict.LEVEL_USER);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> update(@PathVariable("id") Integer id, @RequestBody Dict dict) {
        dictService.update(id, dict.getValue());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) {
        dictService.delete(id);
        return getOkResponseEntity();
    }
}
